package com.sojson.service.impl;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sojson.config.exception.TransException;
import com.sojson.dao.GoodSpecsDao;
import com.sojson.dao.GoodsDao;
import com.sojson.entity.bo.Select;
import com.sojson.entity.po.GoodSpecs;
import com.sojson.entity.po.Goods;
import com.sojson.entity.vo.GoodsVo;
import com.sojson.enums.EGoodsStatus;
import com.sojson.enums.core.ERoleType;
import com.sojson.service.GoodSpecsService;
import com.sojson.service.GoodsService;
import com.sojson.util.StringUtil;
import com.sojson.util.file.FileDepotUtil;
import com.sojson.util.file.constant.ConstantFileFieldName;
import com.sojson.util.file.enums.EFileTable;
import com.sojson.util.token.TokenUtil;

/**
 * 商品表Service实现类
 * 
 * @author liu
 * @date 2021-03-16
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodSpecsService goodSpecsService;
    @Resource
    private GoodsDao goodsDaoImpl;
    @Resource
    private GoodSpecsDao goodSpecsDaoImpl;

    /**
     * 单条插入
     * 
     * @param goods
     * @return
     */
    @Override
    public int insert(Goods goods) {
        int insert = goodsDaoImpl.insert(goods);

        if (insert > 0) {
            FileDepotUtil.insertSync(EFileTable.GOODS, ConstantFileFieldName.THUMB, goods.getThumb());
            FileDepotUtil.insertListSync(EFileTable.GOODS, ConstantFileFieldName.IMAGES, goods.getImages());
        }
        return insert;
    }

    /**
     * 单条插入
     * 
     * @param goods
     * @return
     */
    @Override
    @Transactional
    public int insertOne(GoodsVo goods) {
        int insert = goodsDaoImpl.insert(goods);

        if (insert > 0) {
            FileDepotUtil.insertSync(EFileTable.GOODS, ConstantFileFieldName.THUMB, goods.getThumb());
            FileDepotUtil.insertListSync(EFileTable.GOODS, ConstantFileFieldName.IMAGES, goods.getImages());
        }

        List<GoodSpecs> goodSpecss = goods.getGoodSpecss();
        if (StringUtil.isNotBlankObject(goodSpecss) && goodSpecss.size() > 0) {
            for (GoodSpecs goodSpecs : goodSpecss) {
                goodSpecs.insertInit();
                goodSpecs.setGoodId(goods.getId());
                FileDepotUtil.insertSync(EFileTable.GOOD_SPECS, ConstantFileFieldName.THUMB, goodSpecs.getThumb());
            }
            goodSpecsDaoImpl.insertList(goodSpecss);
        }
        return insert;
    }

    /**
     * 批量插入
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @Override
    public int insertList(Goods goods) {
        int insertList = goodsDaoImpl.insertList(goods.getGoodss());
        return insertList;
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByDto(Goods goods) {
        int deleteByDto = goodsDaoImpl.deleteByDto(goods);
        return deleteByDto;
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public int countAll() {
        int countAll = goodsDaoImpl.countAll();
        return countAll;
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    @Override
    public List<Goods> findAll() {
        List<Goods> findAll = goodsDaoImpl.listAll();
        return findAll;
    }

    /**
     * 根据对象统计数量
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @Override
    public int countByDto(Goods goods) {
        int countByDto = goodsDaoImpl.countByDto(goods);
        return countByDto;
    }

    /**
     * 根据对象查询
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @Override
    public List<Goods> findByDto(Goods goods) {
        String goodTags = goods.getGoodTags();
        if (StringUtil.isNotBlank(goodTags)) {
            goods.setGoodTagIds(goodTags.split(","));
        }
        List<Goods> findByDto = goodsDaoImpl.listByDto(goods);
        return findByDto;
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteById(Integer id) {
        Goods findById = goodsDaoImpl.findById(id);

        GoodSpecs goodSpecs = new GoodSpecs();
        goodSpecs.setGoodId(id);
        goodSpecsService.deleteByDto(goodSpecs);
        int deleteById = goodsDaoImpl.deleteById(id);

        if (deleteById > 0) {
            FileDepotUtil.deleteSync(findById.getThumb());
            FileDepotUtil.deleteListSync(findById.getImages());
        }
        return deleteById;
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public int deleteByIdList(ArrayList<Integer> ids) {
        int deleteByIdList = goodsDaoImpl.deleteByIdList(ids);
        return deleteByIdList;
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @Override
    public int updateById(Goods goods) {
        int updateById = goodsDaoImpl.updateById(goods);
        return updateById;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticByIdList(Goods goods) {
        int updateStaticByIdList = goodsDaoImpl.updateStaticByIdList(goods);
        return updateStaticByIdList;
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangeByIdList(Goods goods) {
        int updateChangeByIdList = goodsDaoImpl.updateChangeByIdList(goods.getGoodss());
        return updateChangeByIdList;
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @Override
    public int updatePartialById(Goods goods) {
        Goods findById = goodsDaoImpl.findById(goods.getId());

        int updatePartialById = goodsDaoImpl.updatePartialById(goods);

        if (updatePartialById > 0) {
            FileDepotUtil.updateSync(EFileTable.GOODS.getCode(), ConstantFileFieldName.THUMB, goods.getThumb(),
                findById.getThumb());
            FileDepotUtil.updateListSync(EFileTable.GOODS.getCode(), ConstantFileFieldName.IMAGES, goods.getImages(),
                findById.getImages());
        }
        return updatePartialById;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @Override
    public int updateStaticPartialByIdList(Goods goods) {
        int updateStaticPartialByIdList = goodsDaoImpl.updateStaticPartialByIdList(goods);
        return updateStaticPartialByIdList;
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @Override
    public int updateChangePartialByIdList(Goods goods) {
        int updateChangePartialByIdList = goodsDaoImpl.updateChangePartialByIdList(goods.getGoodss());
        return updateChangePartialByIdList;
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public Goods findById(Integer id) {
        Goods findById = goodsDaoImpl.findById(id);
        return findById;
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<Goods> findByIdList(ArrayList<Integer> ids) {
        ArrayList<Goods> findByIdList = goodsDaoImpl.findByIdList(ids);
        return findByIdList;
    }

    /**
     * 通过对象查询id列表
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @Override
    public ArrayList<Integer> findIdByDto(Goods goods) {
        ArrayList<Integer> findIdByDto = goodsDaoImpl.findIdByDto(goods);
        return findIdByDto;
    }

    /**
     * 清空数据(物理清空)
     * 
     * @throws SQLException
     */
    @Override
    public void clear() {
        goodsDaoImpl.clear();
    }

    /**
     * 根据对象查询
     * 
     * @param goods
     * @return
     * @throws SQLException
     */
    @Override
    public List<GoodsVo> list(GoodsVo goods) {
        String goodTags = goods.getGoodTags();
        if (StringUtil.isNotBlank(goodTags)) {
            goods.setGoodTags(null);
            goods.setGoodTagIds(goodTags.split(","));
        }
        return goodsDaoImpl.list(goods);
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public Goods getInfo(Integer id) {
        return goodsDaoImpl.getInfo(id);
    }

    /**
     * 根据对象查询
     * 
     * @param goods
     * @return
     */
    @Override
    public List<Select> findSelect(Goods goods) {
        return goodsDaoImpl.findSelect(goods);
    }

    /**
     * 修改商品状态
     * 
     * @param goods
     * @return
     */
    @Override
    public int updateStatus(Goods goods) {
        return goodsDaoImpl.updatePartialById(goods);
    }

    /**
     * 修改商品状态
     * 
     * @param goods
     * @return
     */
    @Override
    public int updateStatusList(Goods goods) {
        return goodsDaoImpl.updateStaticPartialByIdList(goods);
    }

    /**
     * 修改商品状态
     * 
     * @param goods
     * @return
     * @throws IOException 
     */
    @Override
    public int updateStatusApp(Goods goods) throws IOException {
        Goods findById = goodsDaoImpl.findById(goods.getId());

        Set<String> roles = TokenUtil.getRoles();
        if (findById.getStatus() == EGoodsStatus.DISABLE.getCode() && !roles.contains(ERoleType.ADMIN.getKey())
            && !roles.contains(ERoleType.COMMON.getKey())) {
            throw new TransException("商品已禁用,不能修改状态！");
        }

        return goodsDaoImpl.updatePartialById(goods);
    }

    /**
     * 商家首页统计
     * 
     * @return
     */
    @Override
    public Map<String, Integer> count(GoodsVo goods) {
        return goodsDaoImpl.count(goods);
    }

}